const db = require("../common/db/index")

module.exports = {
  // 添加收藏
  add(article_id, user_id, createAt) {
    return new Promise(async (resolve, reject) => {
      try {
        // 先检查是否已收藏
        const checkSql = 'SELECT * FROM pre_favorite WHERE article_id = ? AND user_id = ?';
        const checkResult = await db.query(checkSql, [article_id, user_id]);

        if (checkResult.length > 0) {
          resolve({
            code: 1,
            msg: "已收藏过该文章"
          });
          return;
        }

        // 添加收藏
        const sql = 'INSERT INTO pre_favorite (article_id, user_id, createAt) VALUES (?, ?, ?)';
        const result = await db.query(sql, [article_id, user_id, createAt]);

        resolve({
          code: 200,
          msg: "收藏成功",
          result: result
        });
      } catch (err) {
        reject({
          code: 500,
          msg: "收藏失败",
          err: err
        });
      }
    });
  },

  // 取消收藏
  remove(article_id, user_id) {
    return new Promise(async (resolve, reject) => {
      try {
        const sql = 'DELETE FROM pre_favorite WHERE article_id = ? AND user_id = ?';
        const result = await db.query(sql, [article_id, user_id]);

        resolve({
          code: 200,
          msg: "取消收藏成功",
          result: result
        });
      } catch (err) {
        reject({
          code: 500,
          msg: "取消收藏失败",
          err: err
        });
      }
    });
  },

  // 检查是否已收藏
  check(article_id, user_id) {
    return new Promise(async (resolve, reject) => {
      try {
        const sql = 'SELECT * FROM pre_favorite WHERE article_id = ? AND user_id = ?';
        const result = await db.query(sql, [article_id, user_id]);

        resolve({
          code: 200,
          msg: "查询成功",
          isFavorite: result.length > 0
        });
      } catch (err) {
        reject({
          code: 500,
          msg: "查询失败",
          err: err
        });
      }
    });
  },

  // 获取用户的收藏列表
  getUserFavorites(user_id, page_num, page_size) {
    return new Promise(async (resolve, reject) => {
      try {
        // 先获取总数
        const countSql = 'SELECT count(*) as total FROM pre_favorite WHERE user_id = ?';
        const countResult = await db.query(countSql, [user_id]);
        const total = countResult[0].total;

        // 分页查询
        let page_num2 = ((parseInt(page_num) - 1) * parseInt(page_size));
        let page_size2 = parseInt(page_size);
        const sql = `
                    SELECT f.*, a.title, u.username as author, a.cate, a.cate_name, a.src, a.createAt as article_createAt, a.view_count 
                    FROM pre_favorite f 
                    LEFT JOIN pre_article a ON f.article_id = a.id 
                    LEFT JOIN pre_user u ON a.user_id = u.id 
                    WHERE f.user_id = ? 
                    ORDER BY f.createAt DESC 
                    LIMIT ${page_num2}, ${page_size2}
                `;
        const data = await db.query(sql, [user_id]);

        resolve({
          code: 200,
          msg: "获取收藏列表成功",
          total: total,
          data: data
        });
      } catch (err) {
        reject({
          code: 500,
          msg: "获取收藏列表失败",
          err: err
        });
      }
    });
  },

  // 获取文章的收藏数量
  getArticleFavoriteCount(article_id) {
    return new Promise(async (resolve, reject) => {
      try {
        const sql = 'SELECT count(*) as count FROM pre_favorite WHERE article_id = ?';
        const result = await db.query(sql, [article_id]);

        resolve({
          code: 200,
          msg: "获取收藏数量成功",
          count: result[0].count
        });
      } catch (err) {
        reject({
          code: 500,
          msg: "获取收藏数量失败",
          err: err
        });
      }
    });
  }
}

